package com.back.service.impl;

import cn.hutool.core.util.StrUtil;
import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.mapper.SceneBaseMapper;
import com.back.model.entity.SceneBase;
import com.back.service.SceneBaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 * @description 针对表【SceneBase(场景基础表)】的数据库操作Service实现
 * @createDate 2024-01-01 00:00:00
 */
@Service
@Slf4j
public class SceneBaseServiceImpl extends ServiceImpl<SceneBaseMapper, SceneBase>
        implements SceneBaseService {

    @Override
    public List<SceneBase> getAllScenes() {
        QueryWrapper<SceneBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sceneId");
        return this.list(queryWrapper);
    }

    @Override
    public List<SceneBase> getScenesByType(String sceneType) {
        if (StrUtil.isBlank(sceneType)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景类型不能为空");
        }
        QueryWrapper<SceneBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sceneType", sceneType);
        queryWrapper.orderByAsc("sceneId");
        return this.list(queryWrapper);
    }

    @Override
    public SceneBase getSceneById(String sceneId) {
        if (StrUtil.isBlank(sceneId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景ID不能为空");
        }
        return this.getById(sceneId);
    }

    @Override
    public boolean createScene(SceneBase sceneBase) {
        if (sceneBase == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景信息不能为空");
        }
        if (StrUtil.hasBlank(sceneBase.getSceneId(), sceneBase.getSceneName(), sceneBase.getSceneType())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景ID、名称、类型不能为空");
        }
        
        // 检查场景ID是否已存在
        SceneBase existScene = this.getById(sceneBase.getSceneId());
        if (existScene != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景ID已存在");
        }
        
        return this.save(sceneBase);
    }

    @Override
    public boolean updateScene(SceneBase sceneBase) {
        if (sceneBase == null || StrUtil.isBlank(sceneBase.getSceneId())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景信息或场景ID不能为空");
        }
        
        // 检查场景是否存在
        SceneBase existScene = this.getById(sceneBase.getSceneId());
        if (existScene == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "场景不存在");
        }
        
        return this.updateById(sceneBase);
    }

    @Override
    public boolean deleteScene(String sceneId) {
        if (StrUtil.isBlank(sceneId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "场景ID不能为空");
        }
        
        // 检查场景是否存在
        SceneBase existScene = this.getById(sceneId);
        if (existScene == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "场景不存在");
        }
        
        return this.removeById(sceneId);
    }
}